//
// Created by Administrator on 2023/8/12.
//
#include <vector>
#include <queue>
#include <iostream>

using namespace std;


class Solution {
public:
    vector<int> applyOperations(vector<int> &nums) {
        // 模拟规则描述步骤就好了
        // 把所有的0移动到最后面，等效于把所有的非0数字按顺序提出来
        queue<int> q;
        int n = nums.size();
        for (int i = 0; i < n - 1; i++) {
            if (nums[i] == nums[i + 1]) {
                nums[i] = 2 * nums[i];
                nums[i + 1] = 0;
            }
            if (nums[i] != 0) {
                q.push(nums[i]);
            }
        }
        if (nums[n - 1] != 0) q.push(nums[n - 1]);
        vector<int> ans(n, 0);
        for (int i = 0; !q.empty(); i++) {
            ans[i] = q.front();
            q.pop();
        }
        return ans;
    }

};

int main() {
    Solution s;
    vector<int> nums = {1, 2, 2, 1, 1, 0};
    vector<int> ans = s.applyOperations(nums);
    for (auto &x: ans) cout << x << " ";
    cout << endl;
    return 0;
}